import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue()
  ],
  
  // 路径别名
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@/components': resolve(__dirname, 'src/components'),
      '@/views': resolve(__dirname, 'src/views'),
      '@/api': resolve(__dirname, 'src/api'),
      '@/stores': resolve(__dirname, 'src/stores'),
      '@/utils': resolve(__dirname, 'src/utils')
    }
  },
  
  // 开发服务器配置
  server: {
    host: '0.0.0.0',
    port: 3001,
    open: true,
    cors: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api')
      }
    }
  },
  
  // 构建配置
  build: {
    target: 'es2015',
    outDir: 'dist',
    assetsDir: 'assets',
    sourcemap: false,
    
    // 代码分割策略
    rollupOptions: {
      output: {
        // 手动分包
        manualChunks: {
          // 将 Vue 相关的包单独打包
          vue: ['vue', 'vue-router', 'pinia'],
          // Element Plus 单独打包
          'element-plus': ['element-plus'],
          // ECharts 单独打包
          echarts: ['echarts', 'vue-echarts'],
          // 工具库单独打包
          utils: ['axios', 'dayjs']
        },
        // 文件命名
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: (assetInfo) => {
          const info = assetInfo.name.split('.')
          const extType = info[info.length - 1]
          if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
            return `media/[name]-[hash].${extType}`
          }
          if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
            return `images/[name]-[hash].${extType}`
          }
          if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
            return `fonts/[name]-[hash].${extType}`
          }
          return `assets/[name]-[hash].${extType}`
        }
      }
    },
    
    // 压缩配置
    minify: 'terser',
    terserOptions: {
      compress: {
        // 生产环境移除console.log
        drop_console: true,
        drop_debugger: true,
        pure_funcs: ['console.log']
      },
      format: {
        // 删除注释
        comments: false
      }
    },
    
    // 资源处理
    assetsInlineLimit: 4096, // 小于4kb的资源内联
    
    // 禁用 CSS 代码拆分
    cssCodeSplit: true,
    
    // 构建后移除空的CSS文件
    emptyOutDir: true,
    
    // 报告压缩后的大小
    reportCompressedSize: true,
    
    // chunk 大小警告限制
    chunkSizeWarningLimit: 1000
  },
  
  // CSS 配置
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: (content, filename) => {
          // 排除 variables.scss 文件本身，避免循环引用
          if (filename.includes('variables.scss')) {
            return content;
          }
          return `@use "@/styles/variables.scss" as *;\n${content}`;
        },
        charset: false
      }
    },
    // CSS 模块化
    modules: {
      localsConvention: 'camelCase'
    }
  },
  
  // 预构建优化
  optimizeDeps: {
    include: [
      'vue',
      'vue-router',
      'pinia',
      'element-plus',
      'element-plus/es',
      '@element-plus/icons-vue',
      'echarts',
      'vue-echarts',
      'axios',
      'dayjs'
    ],
    exclude: ['@vueuse/core']
  },
  
  // 环境变量
  define: {
    __VUE_OPTIONS_API__: true,
    __VUE_PROD_DEVTOOLS__: false
  }
})